package com.example.shopping1902.me;


import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.shopping1902.R;
import com.example.shopping1902.utils.BaseFragment;
import com.example.shopping1902.utils.URLUtils;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

/**
 * A simple {@link Fragment} subclass.
 */
public class RegFragment extends BaseFragment implements View.OnClickListener {
    EditText nameEt,pwdEt,repwdEt,telEt;
    Button regBtn;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_reg, container, false);
        initView(view);
        return view;
    }

    private void initView(View v) {
        nameEt = v.findViewById(R.id.frag_reg_et2);
        pwdEt = v.findViewById(R.id.frag_reg_et3);
        repwdEt = v.findViewById(R.id.frag_reg_et4);
        telEt = v.findViewById(R.id.frag_reg_et1);
        regBtn = v.findViewById(R.id.frag_reg_btn);
        regBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        String nickname = nameEt.getText().toString();
        String tel = telEt.getText().toString();
        String pwd = pwdEt.getText().toString();
        String repwd = repwdEt.getText().toString();
        if (!checkinfo(nickname, tel, pwd, repwd)) return;


        Map<String,String>map = new HashMap<>();
        map.put("phone",tel);
        map.put("password",pwd);
        map.put("nickname",nickname);
        map.put("name",tel);
        map.put("address",pwd);
        map.put("question",nickname);
        map.put("answer",nickname);
        //联网
        postNetwork(URLUtils.regURL,map);
    }
    //联网成功时会调用的方法
    @Override
    public void onSuccess(String result) {
        UserBean userBean=new Gson().fromJson(result,UserBean.class);
        if (userBean.getCode()==200){
            //成功之后的操作,跳转登录页面
            String msg="注册成功，是否切换到登录界面";
            showDialog(msg);

        }else if (userBean.getCode()==201){
            String msg="此手机号码已注册，是否切换到登录界面？";
            showDialog(msg);
            return;
        }else{
            Toast.makeText(getContext(), "用户注册失败", Toast.LENGTH_SHORT).show();
        }

    }

    private void showDialog(String msg) {
        AlertDialog.Builder builder=new AlertDialog.Builder(getContext());
        builder.setTitle("提示信息").setMessage(msg);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                ((LoginActivity)getActivity()).changePage();
            }
        });
        builder.setNegativeButton("取消",null);
        builder.create().show();
    }


    private boolean checkinfo(String nickname, String tel, String pwd, String repwd) {
        //判断不能为空
        if (TextUtils.isEmpty(nickname)||TextUtils.isEmpty(tel)||TextUtils.isEmpty(pwd)||TextUtils.isEmpty(repwd)){
            Toast.makeText(getContext(),"输入内容不能为空！",Toast.LENGTH_SHORT).show();
            return false;
        }
        //判断两次输入密码是否相同
        if (!pwd.equals(repwd)){
            Toast.makeText(getContext(),"两次输入密码不一致！",Toast.LENGTH_SHORT).show();
            repwdEt.setText("");//将确认密码清空
            return false;
        }
        //判断电话号码是否符合格式
        String match = "^1(?:3\\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\\d|9\\d)\\d{8}$";
        if (!tel.matches(match)){
            Toast.makeText(getContext(),"输入电话号码格式不对！",Toast.LENGTH_SHORT).show();
            telEt.setText("");//将确认密码清空
            return false;
        }
        return true;
    }
}
